//
//  AJTTItemAddStepOneController.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import UIKit
import ProgressHUD
class AJTTItemAddStepOneController: AJTTBaseViewController {
    let datePicker = UIDatePicker()
    @IBOutlet var bgView: UIView!
    
    @IBOutlet var nameTF: UITextField!
    @IBOutlet var inventionTF: UITextField!
    @IBOutlet var picture: UIImageView!
    @IBOutlet var descTV: UITextView!
    var earlyPic:UIImage? = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        self.descTV.addPlaceholder("Please enter early appearance description", color: .lightGray)
        datePicker.datePickerMode = .date
        if #available(iOS 13.4, *) {
            datePicker.preferredDatePickerStyle = .wheels
        } else {
            // Fallback on earlier versions
        }
        // Do any additional setup after loading the view.
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.bgView.addRoundCorners(topLeftRadius: 15, topRightRadius: 15, bottomLeftRadius: 0, bottomRightRadius: 0, borderWidth: 0, borderColor: .red)
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func clickSaveAction(_ sender: Any) {
    }
    @IBAction func clickNextAction(_ sender: Any) {
        if self.nameTF.text!.isEmpty {
            ProgressHUD.showError("Please enter item name")
            return
        }
        if self.inventionTF.text!.isEmpty {
            ProgressHUD.showError("Please select age of invention")
            return
        }
        if self.earlyPic == nil {
            ProgressHUD.showError("Please select early picture")
            return
        }
        if self.descTV.text!.isEmpty {
            ProgressHUD.showError("Please enter early appearance description")
            return
        }
        let model = AJTTItemModel()
        model.name = self.nameTF.text!
        model.invention = self.inventionTF.text!
        model.earlyPic = UIImage.saveImageInCache(self.earlyPic!)!
        model.earlyDesc = self.descTV.text!
        model.isDefault = false
        let nextVC = AJTTItemAddStepTwoController()
        nextVC.model = model
        self.navigationController?.pushViewController(nextVC, animated: false)
    }
    @IBAction func clickAgeOfInventionAction(_ sender: Any) {
        let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: .actionSheet)
                alertController.view.addSubview(datePicker)
                let doneAction = UIAlertAction(title: "Done", style: .default) { _ in
                    let selectedDate = self.datePicker.date
                    let calendar = Calendar.current
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy"
                    let selectedYear = dateFormatter.string(from: selectedDate)
                    self.inventionTF.text = selectedYear
                }
                alertController.addAction(doneAction)
                present(alertController, animated: true, completion: nil)
    }
    @IBAction func tapEarlyPictureAction(_ sender: Any) {
        showPickImageVC()
    }

}
extension AJTTItemAddStepOneController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func showPickImageVC() {
        let photoPickerViewController:UIImagePickerController = UIImagePickerController()
        photoPickerViewController.sourceType = UIImagePickerController.SourceType.photoLibrary
        photoPickerViewController.delegate = self
        photoPickerViewController.modalPresentationStyle = .fullScreen
        self.present(photoPickerViewController, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        self.picture.image = selectedImage
        self.earlyPic = selectedImage
        picker.dismiss(animated: true, completion: nil)
    }
}
